package com.far.game.xiuzhen.common.utils;

import com.far.game.xiuzhen.common.database.entity.Vo.SelectOptionVo;
import com.far.game.xiuzhen.common.database.entity.Vo.SelectVo;
import com.far.game.xiuzhen.common.enums.game.buf.BufTypeEnums;

import com.far.game.xiuzhen.common.enums.game.SelectVoOfEnumTypeEnum;
import com.far.game.xiuzhen.common.interf.IEnums;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

public class EnumUtil {


    /**
     * @param enumVals
     * @param name
     * @description: 枚举类必须实现IEnum接口
     * @return: int
     * @author mike/Fang.J/Q184377367
     * @date: 2022/8/14 15:21
     */
    public static <T extends IEnums> int valOfName(T[] enumVals, String name) {
        Optional<T> ev = Arrays.stream(enumVals).filter(e -> {
            return e.getName().equals(name);
        }).findFirst();
        //抛出异常 让调用方感知错误
        if (!ev.isPresent())
            throw new IllegalArgumentException("The content corresponding to the enumeration does not exist");
        return ev.get().getVal();
    }

    /**
     * @param enumVals
     * @param val
     * @description:枚举类必须实现IEnum接口
     * @return: java.lang.String
     * @author mike/Fang.J/Q184377367
     * @date: 2022/8/14 15:21
     */
    public static <T extends IEnums> String nameOfVal(T[] enumVals, int val) {
        Optional<T> ev = Arrays.stream(enumVals).filter(e -> {
            return e.getVal() == val;
        }).findFirst();
        if (!ev.isPresent())
            throw new IllegalArgumentException("The content corresponding to the enumeration does not exist");
        return ev.get().getName();
    }


    /**
     * @param enumVals
     * @param type
     * @description: 枚举转成前端可用的 下拉列表
     * @return: com.far.game.xiuzhen.common.database.entity.Vo.SelectVo
     * @author mike/Fang.J/Q184377367
     * @date: 2022/8/14 16:15
     */
    public static <T extends IEnums> SelectVo toSelectVo(T[] enumVals, SelectVoOfEnumTypeEnum type) {
        SelectVo selectVo = new SelectVo();
        selectVo.setType(type.getVal());
        Stream.of(enumVals).forEach(e -> {
            SelectOptionVo selectOptionVo = new SelectOptionVo();
            selectOptionVo.setVal(e.getVal());
            selectOptionVo.setName(e.getName());
            selectVo.addOption(selectOptionVo);
        });
        return selectVo;
    }




    public static void main(String[] args) {

        //枚举转
        SelectVo selectVo = toSelectVo(BufTypeEnums.values(), SelectVoOfEnumTypeEnum.BUF_TYPE);

        //一次性返回N个枚举
        List<SelectVo> selectVoList = new ArrayList<>();
        selectVoList.add(toSelectVo(BufTypeEnums.values(), SelectVoOfEnumTypeEnum.BUF_TYPE));
        //.....

        EnumUtil.valOfName(BufTypeEnums.values(), "眩晕");
        EnumUtil.nameOfVal(BufTypeEnums.values(), 1);
    }

}
